import { makePicLocal } from "../utils/filesBox.js"
import delay from "../utils/delay.js"
import { downloadUrl, removeFile } from "../utils/fs.js"
import request from "request"

const handleCheckPost = async (content, room) => {
  try {

    let key = content.split("#")[1] || content.split('＃')[1]
    // console.log(key.split('*'))
    let rule = `@我说'查快递'并用'#'拼接快递单号，如：'查快递#111'`
    if (!key) {
      room.say(rule)
      return
    }
    
    const res = await getPostRequire(key)
    const data = res.result
    console.log(data)
    const str = `快递信息如下:\n\n单号:${key}\n\n派单状态：${data.status}\n\n物流追踪：`
    let run= ``
    data.info.map(item=>{
      run+=`\n\n${item.time||''}\n${item.context||''}`
    })
    const saying = str + run
    console.log(saying)
    if(saying && room){
      delay(2000)
      room.say(saying)
    }
    
    // console.log(linkUrl)
  } catch (e) {
    console.log(e)
    room.say `查不到这个快递`
  }
}

const getPostRequire=(key)=>{
  console.log(key)
    return new Promise((resolve,roject)=>{
        var options = {
            'method': 'get',
            'url':encodeURI( `https://api.oioweb.cn/api/common/delivery?nu=${key}`),
            'headers': {
              'Content-Type': 'application/json'
            },
        }
        request(options, function (error, response) {
            if (error) resolve(error)
            let data = JSON.parse(response.body)
          // console.log('歌手:'+artist)
          // console.log(data)
            resolve(data)
          })
    })
}


export default handleCheckPost